/*
 * @Author: xuhong
 * @Date: 2020-04-29 20:40:21
 * @LastEditTime: 2020-05-07 10:10:30
 * @FilePath: \my-admin\src\store\modules\asnycRoute.js
 */


import {administrator,otherAdmin} from '../../route/modules/administrator'
import {adminTeacher,otherTeacher} from '../../route/modules/adminTeacher'
import router from '../../route'

const asnycRoute = {
    state:{
        route:'',
        roles:'',
        indexRoute:'/logo',
        menu:''
    },
    getters:{
        menu: state => state.menu,
        allStore: state => state.route,
        roles: state => state.roles,
        indexRoute: state => state.indexRoute
    },
    mutations:{
        SET_ROUTERS: (state, route) => {
            state.route = route // 权限路由
        },
        SET_MENU: (state, menu) => {
            state.menu = menu
        },
        SET_INDEXROUTE: (state, router) => {
            state.indexRoute = router
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        }
    },
    actions: {
        GenerateRoutes({commit},roles){
            return new Promise(resolve => {
                let newRouter = ''
                let menu = ''
                if(roles.roles == 1){
                    newRouter = administrator.concat(otherAdmin)
                    menu = administrator
                } else if (roles.roles == 2){
                    newRouter = adminTeacher.concat(otherTeacher)
                    menu = adminTeacher
                }
                if(menu.length){
                    let index = menu[0].children[0].path
                    commit('SET_INDEXROUTE',index)
                }
                commit('SET_MENU', menu)
                commit('SET_ROUTERS', newRouter)
                commit('SET_ROLES',roles)
                resolve()
            })
        }
    }
}

export default asnycRoute